from socket import *
import hmac

connect_key = b'aaaa'
def connect_auth(conn):
    data = conn.recv(32)
    h = hmac.new(connect_key, data)
    digest = h.hexdigest()
    print(digest)
    conn.sendall(digest.encode("utf-8"))

def connect_to_server(ip_port,buff_size):
    clent_socket=socket(AF_INET,SOCK_STREAM)
    clent_socket.connect(ip_port)
    connect_auth(clent_socket)
    while True:
        try:
            data = input("请输入>>>:").strip()
            if not data: continue
            if data == "quit": break
            clent_socket.sendall(data.encode("utf-8"))
            respone= clent_socket.recv(buff_size)
            print(respone.decode("utf-8"))
        except Exception:
            print(Exception)
    clent_socket.close()

if __name__ == '__main__':
    ip_port = ("localhost", 1234)
    buff_size = 1024
    # connect_to_server()
    connect_to_server(ip_port, buff_size)

